
package com.j2eefast.flowable.bpm.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.flowable.bpm.entity.ProjectArchivesEntity;
import com.j2eefast.flowable.bpm.entity.ProjectSanLianEntity;
import com.j2eefast.flowable.bpm.mapper.ArchivesMapper;
import com.j2eefast.flowable.bpm.mapper.SanLianMapper;
import com.j2eefast.flowable.bpm.utils.StringUtils;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;




/**
 * <p></p>
 *
 * @author: zhangyuan
 * @date: 2021-04-05 23:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */


@Service
//@Transactional(readOnly = true)
public class ArchivesService extends ServiceImpl<ArchivesMapper, ProjectArchivesEntity> {

	@Resource
	protected ArchivesMapper archivesMapper;

	//获取档案最大编号
	public ProjectArchivesEntity getArchivesId(){
		return archivesMapper.getArchivesId();
	}

	public PageUtil archivesList(Map<String, Object> params) {
		Long userId= UserUtils.getUserInfo().getId();
		String contractNo = (String) params.get("contractNo");	//汇款单位
		String fileNumber = (String) params.get("fileNumber");
		String projectName = (String) params.get("projectName");
		Page<ProjectArchivesEntity> page = archivesMapper.archivesList(	new Query<ProjectArchivesEntity>(params).getPage(),
				userId,
				StrUtil.nullToDefault(contractNo,""),
				StrUtil.nullToDefault(fileNumber,""),
				StrUtil.nullToDefault(projectName,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}



	public List<ProjectArchivesEntity> findList(Map<String, Object> params) {
		Long userId= UserUtils.getUserInfo().getId();
		String contractNo = (String) params.get("contractNo");	//汇款单位
		String fileNumber = (String) params.get("fileNumber");
		String projectName = (String) params.get("projectName");
		List<ProjectArchivesEntity> page = archivesMapper.findList(	new Query<ProjectArchivesEntity>(params).getPage(),
				userId,
				StrUtil.nullToDefault(contractNo,""),
				StrUtil.nullToDefault(fileNumber,""),
				StrUtil.nullToDefault(projectName,""),
				(String) params.get(Constant.SQL_FILTER));
		return page;
	}


	public ProjectArchivesEntity getArchivesById(Long archivesId){
		return archivesMapper.getArchivesById(archivesId);
	}


	/**
	 * 添加项目档案
	 * @param taskFrom
	 * @return
	 */
	public boolean save(ProjectArchivesEntity taskFrom){
		String userId= String.valueOf(UserUtils.getUserInfo().getId());
		taskFrom.setUserId(userId);
		String code=DangAnCode(taskFrom.getContractNo());
		taskFrom.setName(code);
		System.out.println(taskFrom.toString());
		return archivesMapper.save(taskFrom);

	}

	/**
	 * 生成档案编码
	 * @param s
	 * @return
	 */
	public String DangAnCode(String s){
		// 对当前日期进行格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSS");
		String date = sdf.format(new Date());
		//DecimalFormat dft = new DecimalFormat(FORMAT_CODE);
		StringUtils ParamUtils = new StringUtils();

		if (s != "" && s != null) {
			String[] str = s.split("-");
			int[] num = new int[str.length];
			for (int i = 0; i < num.length; i++) {
				num[i] = Integer.parseInt(str[i]);
			}
			int i = num[num.length - 1];
			StringBuilder builder = new StringBuilder();
			builder.append("DA-");
			builder.append(date);
			builder.append("-");
			builder.append(i + 1);
			//String code = ParamUtils.getConteactNo(date, i);
			return builder.toString();
		} else {
			int num = 1;
			StringBuilder builder = new StringBuilder();
			builder.append("DA-");
			builder.append(date);
			builder.append("-");
			builder.append(num);
			//String code = ParamUtils.getConteactNo(date, i);
			return builder.toString();
		}
	}


	//根据id获取档案信息
	public ProjectArchivesEntity getArchives(Long archiveId){
		return archivesMapper.getArchives(archiveId);
	}



	public boolean updateByIds(ProjectArchivesEntity projectArchivesEntity){
		return archivesMapper.updateByIds(projectArchivesEntity);
	}


	public boolean deleteArchive(Long[] ids){
		return archivesMapper.deleteArchive(ids);
	}

}
